/**
 * Copyright (C), 2020, Y2 T205
 * FileName: indexController
 * Author: Lenovo
 * Date: 2020-05-20 17:35
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */


package com.atguigu.gulimall.product.web;


import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Lenovo
 * @create 2020-05-20
 * @since 1.0.0
 */
@Controller
public class indexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisson;


//    视图解析器进行配置
//    class：/templates/+返回值+ 。html
    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        //    查出所有的1级分类
     List<CategoryEntity> categoryEntities= categoryService.getLevel1Categorys();

        model.addAttribute("categorys",categoryEntities);
        return "index";
    }


    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String,List<Catelog2Vo>> getCatalogJson(){
       Map<String,List<Catelog2Vo>> map= categoryService.getCatalogJson();
       return map;
    }


    /*测试分布式锁redisson的框架*/
    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
//        好处
/*1.锁的自动续期，不用更担心业务时间过长，而发生的自动删除
* 2.夹锁的业务i只要完成，就不会续期，即使不自动解锁，锁默认在30秒后也会自动删除*/
        /*获取锁，所得名字一样就是一把锁*/
        RLock lock = redisson.getLock("my-lock");
        /*2.枷锁*/
        lock.lock(); //阻塞式等待
        try {
            System.out.println("枷锁成功执行业务"+Thread.currentThread().getId());
            Thread.sleep(3000);
        }catch (Exception e){

        }finally{
            //3.解锁
            System.out.println("释放锁"+Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello";
    }





}
